﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace Model
{
    public class Klienthandler
    {
        //Variabler
        private Socket socket;
        private bool runnning = true;
        NetworkStream networkstream;
        StreamReader reader;
        StreamWriter writer;
        Server server;
        Auktion auktion;
        

        //Konstruktør
        public Klienthandler(Socket socket, Auktion auktion)
        {

            this.socket = socket;
            this.auktion = auktion;
            this.server = server;
            networkstream = new NetworkStream(socket);
            reader = new StreamReader(networkstream);
            writer = new StreamWriter(networkstream);
            Velkomme();
            
        }

        public void Stop()
        {
            runnning = false;
        }



       public void Start()
        {
                while (runnning)
                {
                    //string[] input = reader.ReadLine().Split(' ');

                    string input = reader.ReadLine();

                    //switch (input[0].ToUpper())
                    switch (input.Trim().ToUpper())
                    {
                        case "?":
                            {
                                writer.WriteLine("Command List");
                                writer.WriteLine("EXIT \t\t Afslutter Forbindelsen");
                                writer.WriteLine("HEJ SERVER \t Udskriver hilsen");
                                writer.WriteLine("BUD \t\t Viser ... ");
                                writer.WriteLine("Auktioninfo \t Viser information om auktion");
                                writer.Flush();
                                break;
                            }

                        case "EXIT":
                            {
                                this.Stop();
                                writer.WriteLine("Stopper");
                                writer.Flush();
                                server.StopServer();
                                break;
                            }

                        case "HEJ SERVER":
                            {
                                writer.WriteLine("Hej Klient");
                                writer.Flush();
                                break;
                            }

                        case "BUD":
                            {
                                writer.WriteLine("i'm fat bastard, i'm so sexy!");
                                writer.Flush();
                                break;
                            }

                        case "AUKTIONINFO":
                            {
                                writer.WriteLine("AuktionInfo" + "\r\nPris : " + auktion.AktuelPris);
                                writer.Flush();
                                break;
                            }
                        
                        default:
                            {
                                writer.WriteLine("Ukendt kommando");
                                writer.Flush();
                                break;
                            }
                    }
                }
            }

       public void Velkomme()
       {
           writer.WriteLine("Velkommen til Auktionarius");
           writer.Flush();
       }
        


    }
}
